package com.appview;

import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.model.Teacher;
import com.util.Config;
import com.util.XmlUtil;

import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

/**
 * Created by QiHuang on 2015/10/27.
 * 教师和学生窗口
 */
public class UserView extends JFrame implements MenuListener {
    private JMenuBar menubar;
    private JMenu menu1, menu2,menu3,menu4,menu5, menuGrade;
    private JTable jTable1;
    private JScrollPane jScrollPane;
    private JPanel jPanel, jPanel1;
    private DefaultTableModel tableModel;
    public String accout;
    private static UserView userView;

    public UserView(String accout) {
        this.accout = accout;
        userView = this;
    }

    public static UserView getUserView() {
        return userView;
    }

    public void openView() {
        jScrollPane = new JScrollPane();
        setSize(1000, 600);
        setLocation(200, 80);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        if (Config.getLoginType() == 0) {
            TeacherDao teacherDao = TeacherDao.getInstance();
            setTitle("【"+teacherDao.load(accout).getName() + "】老师,您好");
        } else if (Config.getLoginType() == 1){
            StudentDao studentDao = StudentDao.getInstance();
            setTitle("【"+studentDao.loadByAccount(accout).getName() + "】同学,您好");
        }
        // 创建菜单条和菜单
        menubar = new JMenuBar();
        menubar.setBorderPainted(true);
        menu1 = new JMenu("查看个人信息");
        menuGrade = new JMenu();

        if (Config.getLoginType() == 0) {
            menu3 = new JMenu("班级信息");
            menuGrade = new JMenu("学生成绩");
        }else {
            menu3 = new JMenu("学生个人成绩");
        }

        menu2 = new JMenu("查看课表");
        menu4 = new JMenu("修改密码");
        menu5 = new JMenu("系统操作");

        menubar.add(menu1);
        menubar.add(menu2);
//        menubar.add(menu3);
        menubar.add(menu4);
        menubar.add(menu5);

        add(menubar, BorderLayout.NORTH);
//		add(jPanel, BorderLayout.CENTER);

        //设置按键监听
        menu1.addMenuListener(this);
        menu2.addMenuListener(this);
        menu3.addMenuListener(this);
        menuGrade.addMenuListener(this);
        menu4.addMenuListener(this);
        menu5.addMenuListener(this);

    }

    @Override
    public void menuSelected(MenuEvent e) {
        if (e.getSource() == menu1) {
            //查看个人信息
            ModifyInfo modifyInfo = null;
            if (Config.getLoginType() == 0) {
                TeacherDao teacherDao = TeacherDao.getInstance();
                modifyInfo = new ModifyInfo(0, teacherDao.findByAccount(accout));
            } else {
                StudentDao studentDao = StudentDao.getInstance();
                modifyInfo = new ModifyInfo(1, studentDao.findByAccount(accout));
            }
            modifyInfo.openView();

        }else if (e.getSource() == menu2) {
        //            查看课表
            courseTable();
        } else if (e.getSource() == menu3) {
            //班级信息或个人成绩
        } else if (e.getSource() == menuGrade){
            //教师查看成绩
        } else if (e.getSource() == menu4) {
            //修改密码
            PswModify pswModify = new PswModify(accout);
            pswModify.openView();
        } else if (e.getSource() == menu5) {
            //系统操作
            ExitTip exitTip = new ExitTip();
            exitTip.openView();
        }
    }

    @Override
    public void menuDeselected(MenuEvent e) {

    }

    @Override
    public void menuCanceled(MenuEvent e) {

    }


    //显示教师或学生课表
    private void courseTable() {
        this.remove(jScrollPane);
        this.invalidate();
        jTable1 = TableModel.getCourseTable();
        jTable1.setSelectionBackground(Color.CYAN);
        jScrollPane = new JScrollPane(jTable1);
        if (Config.getLoginType() == 0) {
            TableInfoTools.printCourse(jTable1, TeacherDao.getInstance().load(accout).getName());
        } else {
            TableInfoTools.printCourse(jTable1, " ");
        }

        this.add(jScrollPane, BorderLayout.CENTER);
        repaintView();
    }

    //重画界面
    public void repaintView() {
        this.repaint();
        this.validate();
        this.invalidate();
        this.validate();
    }
}
